var req;
var contentMsgId = "contentmsg";
var titleMsgId = "titlemsg";

var contentValid = false;

var valid_classname = "valid";
var invalid_classname = "invalid";

var lastReplyId = "";
var actionUrl = "messageActionPage.jsp";

var lastStr = "";
function onkeyupHandle(area){
	var value = area.value;
	if(value.length <= 400){
		var fontMsg = document.getElementById("fontlengthmsg");
		lastStr = value;
		var fontInfoStr = fontMsg.innerHTML;
		var index = fontInfoStr.indexOf("/");
		var newInfoStr = value.length + "/" + fontInfoStr.substring(index + 1);
		fontMsg.innerHTML = newInfoStr;						
	}else{
		alert("长度不能大于400");
		area.value = lastStr;
	}
}

/**
 * 信件内容验证
 */
function sendSystemMessage(titleId, contentId) {
    sendMessage(titleId, contentId, null, "system");
}

function systemMsgCallback() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			sendSystemMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function sendSystemMsg(msg) {
	setPrivateMsg(msg);
}

/**
 * 信件内容验证
 */
function sendPrivateMessage(button,targetId,titleId,contentId) {
    sendMessage(titleId, contentId, targetId, "private");
}

function sendMessage(titleId,contentId,targetId, type){
	var contentField = document.getElementById(contentId);
	var titleField = document.getElementById(titleId);
	if(!notEmpty(titleField)){
		setMessage(titleMsgId, "标题不能为空", invalid_classname);
		titleValid = false;
		return;
	}
	if(!notEmpty(contentField)){
		setMessage(contentMsgId, "内容不能为空", invalid_classname);
		contentValid = false;
		return;
	}
	var url = "";
	var msgCallback = null;
	var content = contentField.value;
	var title = titleField.value;
	if(type == "private"){
		var targetField = document.getElementById(targetId);
		var target = targetField.value;
		url = actionUrl + "?action=send&title=" + title+ "&target="+ targetField.value + "&" + "content=" + content;
		msgCallback = privateMsgCallback;
	}else{
		url = "adminViews/message/systemMsgActionPage.jsp?action=send&content=" + content + "&title=" + title;
		msgCallback = systemMsgCallback;
	}
	
    sendAsynCall(url, msgCallback);
}

function getRequest(){
	if(window.XMLHttpRequest){
    	//针对FireFox，MOzillar，Opear，Safari，IE7，IE8这些浏览器
    	req = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
    	//针对IE5，IE6 
    	//两个可以用于创建XMLHttpRequest对象的控件名称，保存在一个js的数组中 
    	var activeName = [ "MSXML2.XMLHTTP","Microsoft.XMLHTTP" ];
    	//= new ActiveXObject(""); 
    	for ( var i = 0; i < activeName.length; i++ ){ 
	    	//取出一个空间名进行创建，如果创建成功，种植 
	    	//如果创建失败，则抛出异常，然后可以继续循环，继续尝试创建 
	    	try { 
	    		req = new ActiveXObject( activeName[i] ); 
	    	} catch ( e ) { 
	    	} 
    	}
    }
	
	return req;
}

function privateMsgCallback() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			setPrivateMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function setPrivateMsg(msg) {
	resetInput();
	if (msg == "validate") {
		alert("发送成功！");
	} else {
		alert("发送失败！");
	}
}

/**
 * 验证输入值是否为空
 * @param item 输入框对象
 * @returns {Boolean}
 */
function notEmpty(item){
	var value = escape(item.value);
	if(value == "")
		return false;
	return true;
}

/**
 * 设置提示信息
 * @param itemId 对象的Id
 * @param msg 提示字数串
 * @param className 样式名称
 */
function setMessage(itemId, msg, className){
	var mdiv = document.getElementById(itemId);
	mdiv.className = className;
	mdiv.innerHTML = msg;
}

function resetInput(){
	var content = document.getElementById("contentId");
	var title = document.getElementById("title");
	var fontlengthmsg = document.getElementById("fontlengthmsg");
	var targetSelect = document.getElementById("targetSelect");
	if(content != null)
		content.value = "";
	if(title != null)
		title.value = "";
	if(fontlengthmsg != null)
		fontlengthmsg.innerHTML = "0/400字";
	if(targetSelect != null)
		targetSelect.selectedIndex = 0;
}

function readMesage(field, messageId){
	var url = actionUrl + "?action=read&message=" + messageId;
	sendAsynCall(url, null);
	var oldInnerHTML = field.innerHMTL;
	var newInnerHTML = oldInnerHTML;
	if(oldInnerHTML.indexof("<b>") == 0)
		newInnerHTML = oldInnerHTML.substring(3, oldInnerHTML.length - 3);
	field.innerHMTL = newInnerHTML;
}

function replyMessage(replyId, sourceId){
	var targetDiv = document.getElementById(replyId);
	if(lastReplyId != null && lastReplyId != "" && lastReplyId != replyId){
		var lastReplyDiv = document.getElementById(lastReplyId);
		lastReplyDiv.style.display = "none";
		lastReplyDiv.innerHTML = "";
	}
	var displayStyle = "none";
	var innerValue = "";
	if(targetDiv.style.display == "none"){
		lastReplyId = replyId;
		displayStyle = "block";
		innerValue = "<textarea id='contentId' style='width:400;height:40' onkeyup='onkeyupHandle(this)'></textarea>" +
                "<p><button onclick='replyMessage(" + replyId + ", "+ sourceId +");'>收起回复</button>" +
                "<span id='fontlengthmsg' style='right:right;'>0/400字</span><span style='right:right;'><button type='submit' " +
                "style='right:right;' onclick='sendReplyMessage(" + replyId + ","+ sourceId +");'>发送</button></p>";
	}
	setDivState(targetDiv, displayStyle, innerValue);
	if(innerValue != ""){
		var item = document.getElementById("contentId");
		item.focus();
	}
}

function sendReplyMessage(replyId, sourceId){
	var contentField = document.getElementById("contentId");
	var content = escape(contentField.value);
	if(content == null || content == ""){
		alert("回复内容不能为空");
		return;
	}
	
	var url = actionUrl + "?action=reply&messageId=" + replyId + "&source=" + sourceId + "&content=" + content;
	sendAsynCall(url, replyMsgCallBack);
	var targetDiv = document.getElementById(replyId);
	setDivState(targetDiv, "none", "");
}

function replyMsgCallBack(){
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			replyMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function deleteMessage(id){
	var flag = confirm("你确定要删除");
	if(flag){
		var url = actionUrl + "?action=delete&messageId=" + id;
		sendAsynCall(url, deleteMsgCallback);
	}
}

function deleteSendMessage(id){
	var flag = confirm("你确定要删除");
	if(flag){
		var url = actionUrl + "?action=sendDelete&messageId=" + id;
		sendAsynCall(url, deleteMsgCallback);
	}
}

function deleteMsgCallback() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var msg = req.responseXML.getElementsByTagName("msg")[0];
			sendDelMsg(msg.childNodes[0].nodeValue);
		}
	}
}

function sendDelMsg(msg){
	resetInput();
	if (msg == "validate") {
		location.reload();
	} else {
		alert("删除失败！");
	}
}

function replyMsg(msg){
	if (msg == "validate") {
		alert("回复成功！");
	} else {
		alert("回复失败！");
	}
}

function setDivState(divItem, display, value){
	divItem.style.display = display;
	divItem.innerHTML = value;
}
 
function sendAsynCall(url, callBack){
	req = getRequest();
	var encodeUrl = encodeURI(encodeURI(url));
    req.open("GET",encodeUrl,true);
    req.onreadystatechange = callBack;
    req.send(null);
}	
